// 导入store
import store from '@/store'

// showLoadingStatus 1显示loaging 0不显示
const request = (options = {}, data = {}, showLoadingStatus=0) => {
	let url = store.state.baseUrl + options.path
	let method = options.method.toLowerCase()
	let token = store.state.token
	let contenttype = options.contenttype
	let header = ''
	if (method == 'get') {
		header = {'Authorization': token}
	} 
	if (method == 'post') {
		header = {
			'Authorization': token,
			'Content-Type': contenttype
		}
	}
	if (showLoadingStatus) {
	 uni.showLoading()
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: method,
			header: header,
			data: data
		}).then(response => {
			if (showLoadingStatus) {
				uni.hideLoading()
			}
			let [err, res] = response;
			let data = res.data
			if (data) {
				resolve(data)
			}
			else {
				resolve('')
			}
		}).catch(err => {
			uni.hideLoading()
			uni.showToast({
				title: '网络异常，请稍后再试',
				duration: 1500,
				mask: false,
				icon: 'none'
			});
		})
	})
}

export default {
	request
}